import qiniu
from django.conf import settings
from qiniu import put_data


def upload_avatar(file_data):
    # 上传后保存的文件名
    access_key = settings.QINIU_ACCESS_KEY
    secret_key = settings.QINIU_SECRET_KEY
    # 设置七牛空间(自己刚刚创建的)
    bucket_name = settings.QINIU_BUCKET_NAME
    # 构建鉴权对象,授权
    q = qiniu.Auth(access_key, secret_key)
    # 生成token
    token = q.upload_token(bucket_name)
    ret, info = put_data(token, None, file_data)

    return ret['key']